import java.util.ArrayDeque;
import java.util.Vector;

import junit.framework.TestCase;


public class ROITest extends TestCase {
	public void testAddNeighbors() throws Exception {
		// 000     000
		// 010 --> 011
		// 000     111
		int controls [] = {6, 7, 8, 9};
		assertNeighborsEquals(controls, 5 - 1);

		// 000     000
		// 100 --> 110
		// 000     110
		int controls1 [] = {5, 7, 8};
		assertNeighborsEquals(controls1, 4 - 1);
		
		// 000     000
		// 001 --> 001
		// 000     011
		int controls2 [] = {8, 9};
		assertNeighborsEquals(controls2, 6 - 1);

		// 000     000
		// 000 --> 000
		// 100     110
		int controls3 [] = {8};
		assertNeighborsEquals(controls3, 7 - 1);

		// 000     000
		// 000 --> 000
		// 010     011
		int controls4 [] = {9};
		assertNeighborsEquals(controls4, 8 - 1);

		// 000     000
		// 000 --> 000
		// 001     001
		int controls5 [] = {};
		assertNeighborsEquals(controls5, 9 - 1);
	}

	private void assertNeighborsEquals(int[] controls, int pixel_index) {
		ArrayDeque<Integer> index_of_neighbors = new ArrayDeque<Integer>();
		ROI.addNeighbors(pixel_index, index_of_neighbors, 3, 3);
		assertEquals(controls.length, index_of_neighbors.size());
		for(int control:controls) {
			assertTrue(index_of_neighbors.contains(control - 1));	
		}
	}
	
	public void testMassDetect() throws Exception {
		int one_mass[] = {
				0,0,0,0,0,0,0,0,0,0, //0
				0,0,0,0,0,0,0,0,0,0, //1
				0,0,0,0,0,0,0,0,0,0, //2
				0,0,0,0,0,0,0,0,0,0, //3
				0,0,0,0,1,0,0,0,0,0, //4
				0,0,0,0,0,0,0,0,0,0, //5
				0,0,0,0,0,0,0,0,0,0, //6
				0,0,0,0,0,0,0,0,0,0, //7
				0,0,0,0,0,0,0,0,0,0, //8
				0,0,0,0,0,0,0,0,0,0  //9
				};
		assertMassesDetected(1, one_mass, "one_mass.gif");
		int one_mass_complicated[] = {
				0,0,0,0,0,0,0,0,0,0, //0
				0,0,0,0,0,0,0,0,0,0, //1
				0,0,0,0,0,0,0,0,0,0, //2
				0,0,0,0,0,0,0,0,0,0, //3
				0,0,0,0,1,1,0,0,0,0, //4
				0,0,0,0,0,1,0,0,0,0, //5
				0,0,0,0,0,0,1,0,0,0, //6
				0,0,0,0,0,0,0,0,0,0, //7
				0,0,0,0,0,0,0,0,0,0, //8
				0,0,0,0,0,0,0,0,0,0  //9
				};
		assertMassesDetected(1, one_mass_complicated, "one_mass_complicated.gif");
		int four_masses[] = {
				0,0,0,0,0,0,0,0,0,0, //0
				0,0,0,0,0,0,0,0,0,1, //1
				0,0,0,0,0,0,0,0,1,1, //2
				0,0,0,0,0,0,0,0,0,1, //3
				0,0,0,0,2,2,0,0,0,0, //4
				0,0,0,0,0,2,0,0,0,0, //5
				0,0,0,0,0,0,2,0,0,0, //6
				0,0,0,0,0,0,0,0,0,0, //7
				3,0,0,0,0,4,4,4,0,0, //8
				3,3,0,0,0,0,0,0,0,0  //9
				};
		assertMassesDetected(4, four_masses, "four_masses.gif");
	}

	private void assertMassesDetected(int count, int[] control_pixel_array, String test_image_filename) {
		ROI test_roi = new ROI(test_image_filename);
		ImageData labeled_image = test_roi.massDetect(test_roi.getOriginalImage());

		int[] pixels = labeled_image.allocPixels();
		labeled_image.getPixels(pixels, 0, 0, labeled_image.getWidth(), labeled_image.getHeight());

		for(int i=0; i<control_pixel_array.length; i++) {
			assertEquals(control_pixel_array[i], Util.getRGBavg(pixels[i]));
		}
			
		test_roi.setLabeledImage(labeled_image);
		test_roi.findMasses();
		Vector<Mass> masses = test_roi.getMasses();
		assertEquals(count, masses.size());
	}
}
